#include "GameManagement.h"
#include "GameMenuState.h"
#include "GameRunState.h"
#include "GameDeviceSingleton.h"

GameManagement::GameManagement(int state, 
	LPDIRECT3DDEVICE9 &d3ddv,
	LPD3DXSPRITE &spriteHandler,
	LPDIRECT3DSURFACE9 &background)
{
	_d3ddv = &d3ddv;
	_spriteHandler = &spriteHandler;
	_background = &background;

	// Init singleton data
	GameDeviceSingleton::initDevice(_d3ddv, _spriteHandler, _background);

	/////////////////
	_gameMenu = new GameMenu(this);
	_timeMenuCounter = 0;

	////////////////
	_gameRun = new GameRun(this);
	_timeRunCounter = 0;

	_initGameState();
	setState(state);
}

GameMenu* GameManagement::getGameMenu() {
	return _gameMenu;
}

void GameManagement::setBackground(int state)
{
	*_background = CreateSurfaceFromFile(*_d3ddv, _currentState->getBackgroundPath());
}

GameManagement::~GameManagement()
{
}

void GameManagement::_initGameState()
{
	_states = new vector<GameState*>();
	_states->push_back(new GameMenuState(this));
	_states->push_back(new GameRunState(this));
}

void GameManagement::setState(int state)
{
	_currentState = _states->at(state);
	setBackground(state);
}

void GameManagement::draw()
{
	_currentState->draw();
}

void GameManagement::update()
{
	if (_timeMenuCounter++ > _TIME_UPDATE)
	{
		_currentState->update();
		_timeMenuCounter = 0;
	}
}

void GameManagement::setInput(int keycode, bool onHold)
{
 	_currentState->setInput(keycode, onHold);
}

void GameManagement::drawGameMenu()
{
	_gameMenu->draw();
}

void GameManagement::updateGameMenu()
{
	_gameMenu->update();
}

void GameManagement::drawGameRun()
{
	_gameRun->draw();
}

void GameManagement::updateGameRun()
{
	_gameRun->update();
}